package com.aw.guiguzi.module.manager.transaction.adapter

import android.app.Activity
import android.content.Intent
import android.view.View
import androidx.recyclerview.widget.RecyclerView
import com.aw.baselib.constants.Extras
import com.aw.baselib.util.AwDisplayUtil
import com.aw.baselib.util.AwRecyclerViewUtil
import com.aw.guiguzi.R
import com.aw.guiguzi.bean.TransactionItemBean
import com.aw.guiguzi.module.common.CommonImgActivity
import com.aw.guiguzi.util.AppCacheUtil
import com.chad.library.adapter.base.BaseQuickAdapter
import com.chad.library.adapter.base.BaseQuickAdapter.OnItemClickListener
import com.chad.library.adapter.base.BaseViewHolder
import java.io.Serializable

class ManagerTransactionAdapter :
    BaseQuickAdapter<TransactionItemBean, BaseViewHolder>(R.layout.adapter_manager_transaction) {
    private var mList: MutableList<TransactionItemBean>? = ArrayList()

    init {
        if (mList == null) {
            mList = ArrayList()
        }
    }

    override fun convert(helper: BaseViewHolder, bean: TransactionItemBean) {
        helper.setText(R.id.tv_title, bean.title)
        val rcv_data = helper.getView<RecyclerView>(R.id.rcv_data)
        val imgAdapter = ManagerTransactionImgAdapter()
        AwRecyclerViewUtil.setRecyclerViewGridlayout(
            mContext as Activity,
            rcv_data,
            imgAdapter,
            AppCacheUtil.getGridImgNums(),
            AwDisplayUtil.dip2px(mContext, 2f)
        )
        imgAdapter.addAllData(bean.list)
        imgAdapter.onItemClickListener =
            OnItemClickListener { adapter: BaseQuickAdapter<*, *>?, view: View?, position: Int ->
                val intent = Intent(mContext, CommonImgActivity::class.java)
                intent.putExtra(Extras.params_int, position)
                intent.putExtra(Extras.params, bean.list as Serializable)
                mContext.startActivity(intent)
            }
    }

    fun addAllData(dataList: MutableList<TransactionItemBean>?) {
        mList = dataList
        setNewData(mList)
        notifyDataSetChanged()
    }

    fun clearData() {
        if (mList != null) {
            val startPosition = 0 //hasHeader is 1
            val preSize = mList!!.size
            if (preSize > 0) {
                mList!!.clear()
                notifyItemRangeRemoved(startPosition, preSize)
            }
        }
    }
}